<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>简易计算器</title>
    <style>
        .calculator {
            width: 320px;
            margin: 20px auto;
            padding: 20px;
            background: #f0f0f0;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.2);
        }

        #display {
            width: 100%;
            height: 60px;
            margin-bottom: 10px;
            padding: 10px;
            font-size: 24px;
            text-align: right;
            border: none;
            border-radius: 5px;
            background: white;
        }

        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 5px;
        }

        button {
            padding: 15px;
            font-size: 18px;
            border: none;
            border-radius: 5px;
            background: white;
            cursor: pointer;
            transition: 0.3s;
        }

        button:hover {
            background: #e0e0e0;
        }

        .operator {
            
            color: white;
        }

        .equal {
            
            color: white;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <input type="text" id="display" readonly>
        <div class="buttons">
            <button class="operator">C</button>
            <button class="operator">←</button>
            <button class="operator">%</button>
            <button class="operator">/</button>
            <button>7</button>
            <button>8</button>
            <button>9</button>
            <button class="operator">*</button>
            <button>4</button>
            <button>5</button>
            <button>6</button>
            <button class="operator">-</button>
            <button>1</button>
            <button>2</button>
            <button>3</button>
            <button class="operator">+</button>
            <button>0</button>
            <button>.</button>
            <button class="equal" style="grid-column: span 2">=</button>
        </div>
    </div>

    <script>
        const display = document.getElementById('display');
        const buttons = document.querySelector('.buttons');

        // 事件委托处理按钮点击
        buttons.addEventListener('click', (e) => {
            if (!e.target.matches('button')) return;
            
            const btn = e.target;
            const btnText = btn.textContent;
            const current = display.value;

            switch(btnText) {
                case 'C':
                    display.value = '';
                    break;
                case '←':
                    display.value = current.slice(0, -1);
                    break;
                case '=':
                    try {
                        // 处理计算精度问题
                        const result = evaluateExpression(current);
                        display.value = result === undefined ? 'ERROR' : result;
                    } catch {
                        display.value = 'ERROR';
                    }
                    break;
                default:
                    display.value += btnText;
            }
        });

        function evaluateExpression(expr) {
            // 安全验证
            if (!/^[0-9+\-*/.()%]+$/.test(expr)) return 'ERROR';
            
            try {
                // 处理浮点运算精度
                const result = new Function('return ' + expr)();
                if (!isFinite(result)) return 'ERROR';
                
                // 精确到15位小数并去除末尾的零
                return result.toFixed(15)
                    .replace(/\.?0+$/, '')
                    .replace(/(\.\d*?[1-9])0+$/, '$1');
            } catch {
                return 'ERROR';
            }
        }
    </script>
</body>
</html>